package dados;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

//VS4E -- DO NOT REMOVE THIS LINE!
public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel0;
	private JLabel jLabel1;
	private JButton jButton0;
	private JLabel jLabel2;
	private JTextField jTextField0;
	private JTextField jTextField1;
	private JTextField jTextField2;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JLabel jLabel5;
	public GUI(String nome1, String nome2) {
		initComponents();
		jLabel3.setText(nome1);
		jLabel4.setText(nome2);
		jLabel4.setVisible(false);
	}
	public GUI() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJButton0(), new Constraints(new Trailing(12, 12, 12), new Trailing(12, 156, 156)));
		add(getJTextField0(), new Constraints(new Leading(35, 162, 10, 10), new Leading(52, 12, 12)));
		add(getJTextField1(), new Constraints(new Leading(35, 162, 12, 12), new Leading(80, 12, 12)));
		add(getJTextField2(), new Constraints(new Leading(35, 162, 12, 12), new Leading(112, 12, 12)));
		add(getJLabel1(), new Constraints(new Leading(202, 12, 12), new Leading(82, 50, 50)));
		add(getJLabel0(), new Constraints(new Leading(202, 12, 12), new Leading(54, 15, 50, 50)));
		add(getJLabel2(), new Constraints(new Leading(202, 12, 12), new Leading(114, 50, 50)));
		add(getJLabel4(), new Constraints(new Leading(35, 12, 12), new Leading(144, 12, 12)));
		add(getJLabel3(), new Constraints(new Leading(35, 12, 12), new Leading(170, 10, 10)));
		add(getJLabel5(), new Constraints(new Leading(90, 10, 10), new Leading(14, 10, 10)));
		add(getJTextField3(), new Constraints(new Leading(35, 283, 10, 10), new Leading(204, 12, 12)));
		setSize(543, 270);
	}
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
		}
		return jTextField3;
	}
	private JLabel getJLabel5() {
		if (jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("Jogador 1");
			jLabel5.setVisible(false);
		}
		return jLabel5;
	}
	private JLabel getJLabel3() {
		if (jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("Jogador 2");
		}
		return jLabel3;
	}

	private JLabel getJLabel4() {
		if (jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Jogador 1");
		}
		return jLabel4;
	}

	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
		}
		return jTextField2;
	}

	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
		}
		return jTextField1;
	}

	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
		}
		return jTextField0;
	}

	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Resultado");
		}
		return jLabel2;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("Rolar Dados");
			jButton0.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton0ActionActionPerformed(event);
				}
			});
		}
		return jButton0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Segundo Dado");
		}
		return jLabel1;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Primeiro Dado");
		}
		return jLabel0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				GUI frame = new GUI();
				frame.setDefaultCloseOperation(GUI.EXIT_ON_CLOSE);
				frame.setTitle("GUI");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private int contador=0;
	private JTextField jTextField3;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	private void jButton0ActionActionPerformed(ActionEvent event) {
		
		int dado1=0,dado2=0,resultado=0;
		dado1 = (int) ((int) 1 + (Math.random() * 6));
		dado2 = (int) ((int) 1 + (Math.random() * 6));
		//dado1 = 3;
		//dado2 = 3;
		resultado = dado1 + dado2;
		
		if(!(dado1 == dado2)){
			if(jLabel5.getText().equals("Jogador 1")){
				jLabel4.setVisible(false);
				jLabel3.setVisible(true);
				jLabel5.setText("Jogador 2");
				contador = 0;
			}else
			if(jLabel5.getText().equals("Jogador 2")){
				jLabel4.setVisible(true);
				jLabel3.setVisible(false);
				jLabel5.setText("Jogador 1");
				contador = 0;
			}
		}
		if(dado1 == dado2){
			contador = contador + 1;
			if(contador == 3){
					if(jLabel5.getText().equals("Jogador 1")){
						jLabel4.setVisible(false);
						jLabel3.setVisible(true);
						jLabel5.setText("Jogador 2");
					}else
					if(jLabel5.getText().equals("Jogador 2")){
						jLabel4.setVisible(true);
						jLabel3.setVisible(false);
						jLabel5.setText("Jogador 1");
					}
					contador = 0;
				}
			
		}
		jTextField3.setText("resta " + Integer.toString( 3 - contador) + " jogadas");
		jTextField0.setText(Integer.toString(dado1));
		jTextField1.setText(Integer.toString(dado2));
		jTextField2.setText(Integer.toString(resultado));
	}

}
